const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
//获取应用实例
const app = getApp()
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },

  bindGetUserInfo: function(e) {
    let that = this;
    wx.login({
      success: function (response) {
        if (response.code) {
          //登录远程服务器
            util.request(api.AuthLoginByWeixin, {
              code: response.code,
              userInfo: e.detail
            }, 'POST', 'application/json').then(res => {
              if (res.success == true) {
                //存储用户信息
                wx.setStorageSync('userInfo', res.result.userInfo);
                wx.setStorageSync('token', res.result.token);
                wx.setStorageSync('userId', res.result.userId);
                //返回上一页面
                wx.navigateBack({
                  delta: 1,
                  fail:function(){
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                  }
                })

              } else {
                // util.showErrorToast(res.errmsg)
                wx.showModal({
                  title: '提示',
                  content: res.message,
                  showCancel: false
                });
              }
            });
        }
      },
      fail:function(res){
          console.log(res);
      }
    });
   

  },
  onReady: function() {
    // 页面渲染完成
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },

  onLoad: function (options) {
    //页面加载

  },
  onShow: function () {
    //进入此页面 清空本地存储
    try {
      wx.removeStorageSync('userInfo')
    } catch (e) {
      // Do something when catch error
    }
    try {
      wx.removeStorageSync('token')
    } catch (e) {
      // Do something when catch error
    }
    try {
      wx.removeStorageSync('userId')
    } catch (e) {
      // Do something when catch error
    }
  },
})